<template>
  <div style="display: flex;flex-direction: column;">

    <div class="search">
      <el-input v-model="question" placeholder="请输入要搜索的问答" style="width: 200px;margin-left: 10px;">

        <el-option v-for="item in categoryList" :key="item.id" :label="item.title" :value="item.title">
        </el-option>
      </el-input>
      <el-button type="primary" icon="el-icon-search" circle @click="load"></el-button>
    </div>

    <div class="content1" style="position: relative; display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 20px; padding: 20px;">
  <div 
    v-for="item in tableData" 
    :key="item.id" 
    class="item" 
    @click="previewPic(item)"
    style="
      position: relative;
      border-radius: 8px;
      overflow: hidden;
      box-shadow: 0 2px 12px rgba(0,0,0,0.1);
      transition: transform 0.3s ease;
      cursor: pointer;
      background: #fff;
    "
    @mouseover="this.style.transform = 'scale(1.02)'"
    @mouseout="this.style.transform = 'scale(1)'"
  >
    <div class="title" style="
      padding: 15px;
      font-size: 16px;
      color: #333;
      font-weight: 500;
      line-height: 1.4;
      min-height: 60px;
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 2;
      overflow: hidden;
    ">
      <span>{{ item.question }}</span>
    </div>
    
    <img 
      class="item-img" 
      :src="item.questionAvatar" 
      alt=""
      style="
        width: 100%;
        height: 200px;
        object-fit: cover;
        border-bottom: 1px solid #eee;
      "
    >
    
    <div style="
      position: absolute;
      top: 10px;
      right: 10px;
      display: flex;
      flex-direction: column;
      align-items: flex-end;
      gap: 8px;
    ">
      <el-tag 
        v-if="item.isMy === '1'" 
        type="success" 
        style="font-size: 14px; padding: 4px 8px;"
      >
        我的
      </el-tag>
      <el-tag 
        type="info" 
        style="font-size: 14px; padding: 4px 8px; background: rgba(255,255,255,0.9);"
      >
        回答人数：{{ item.answerTotal }}
      </el-tag>
    </div>
  </div>
</div>
    <!-- 分页效果 -->
    <el-pagination class="page" @current-change="handleCurrentChange" :current-page.sync="currentPage" background
      :page-size="10" layout="prev, pager, next, jumper" :total="total">
    </el-pagination>
    <el-dialog :visible.sync="bigImgVisible" width="60%" :before-close="handleClose">
      <div>
        <el-card class="box-card">
          <div style="display: flex; flex-direction: column;">
            <div class="header">
              <div class="avatar">
                <img :src="currentItem.userAvatar" alt="">
              </div>
              <div class="header-right">
                <p class="name">{{ currentItem.userName }}</p>
                <span class="time">发表时间:{{ currentItem.createdAt }}</span>
                <el-button type="primary" @click="goMessage()" icon="el-icon-s-comment" >私聊作者</el-button>
              </div>
            </div>
            <div style="margin-top: 10px;">
              <el-tag type="success" style="font-size: 16px;">
                <i class="el-icon-s-opportunity"></i>
                {{ currentItem.question }}
              </el-tag>
            </div>
          </div>

        </el-card>
        <el-card class="box-card">
          <div class="description">
            <el-image style="width: 200px; height: 200px" :src="currentItem.questionAvatar" fit="cover" />
          </div>
        </el-card>
        <el-card class="box-card">
          <div class="pushInput">
            <span>发表评论</span>
            <el-input v-model="inputValue" placeholder="请输入评论" />
            <el-upload action="#" :file-list="fileList" list-type="picture-card" :limit="1" :on-change="handelChange"
            :data="dataList" :auto-upload="false">
            <i slot="default" class="el-icon-plus"></i>
          </el-upload>
            <div style="display: flex;justify-content: flex-end;">
              <el-button type="primary" @click="comment">发表</el-button>
            </div>

          </div>
        </el-card>

        <div class="description">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>全部评论</span>
            </div>
            <div class="comment" v-if="commentItem.length > 0">
              <div class="comment-item" v-for="item in commentItem" :key="item" style="position: relative;">
               <div class="comment-item " >
                  <div class="comment-item-left">
                    <img :src="item.userAvatar" alt="">
                  </div>
                  <div class="comment-item-right">
                    <div class="comment-item-right-top">
                      <span>{{ item.username }}</span>
                    </div>
                    <div class="comment-item-right-bottom">
                      <span>{{ item.createdAt }}</span>
                    </div>
                    <div style="display: flex; justify-content: center;">
                      <img src="../../assets/img/reply.png" style="width: 24px;margin-right: 5px;" alt="">
                      <span>{{ item.answer }}</span>
                    </div>
                  </div>
               </div>
                <el-image style="width: 100px; height: 100px" :src="item.answerAvatar" fit="cover" />
                <div class="comment-item-buttom">
                  <el-button type="primary" @click="goMessage1(item)" icon="el-icon-s-comment" >私聊</el-button>
                </div>
              </div>

            </div>
            <div v-else class="comment-none">
              <div><span>还没有评论</span></div>
              <img
                src="https://static.nowcoder.com/fe/file/site/www-web/prod/1.0.384/imageAssets/b725ace1c6d26c431db0.png"
                alt="">
            </div>
          </el-card>
        </div>
      </div>
      <el-pagination @current-change="handleCurrentChange" :current-page.sync="currentPage" background :page-size="10"
        layout="prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-dialog>
  </div>
</template>
<script>

import { getQuestionList, getQuestionAnswerDetail, getQuestionAnswer } from '@/http/answer.js'
import { mapGetters } from 'vuex'
import { getUpload } from '@/http/upload.js'

export default {

  data() {
    return {
      currentPage: 1,
      //当前点击页码数
      pageSize: 10,
      //每页展示数目
      total: 0,
      multipleSelection: [],
      // 被选中的列表数据
      bigImgVisible: false,
      hospitalList: [],
      tableData: [],
      dataList: {
        
      },
      srcList: [],
      currentItem: {},
      inputValue: '',
      commentItem: [],
      currentPage1: 1,
      answerAvatar: '',
      fileList: [],
      //每页展示数目
      total1: 0,
      question: '',
      categoryList: [],
      numbers: 0,
      dialogVisible: false,
      option: {
        img: '', // 裁剪图片的地址
        info: true, // 裁剪框的大小信息
        infoTrue: true, // 真实的输出宽高
        autoCrop: true, // 是否自动裁剪
      }
    };
  },
  mounted() {
    this.load()
  },
  computed: {
    ...mapGetters({
      userRole: 'user/getUserRole',
      userId: 'user/getUserId',
      userName: 'user/getUserName',

    })
  },
 
  //进入页面请求报告列表数据
  methods: {
    goMessage1(item) {
      console.log(item);
      
      this.$router.push({
        path: '/chat',
        query: {
          sendId: this.userId,
          redaId: item.userId,
          sendName: this.userName,
          redaName: item.username,
        }
      })
    },
    goMessage(item) {
      console.log(this.currentItem);
      
      this.$router.push({
        path: '/chat',
        query: {
          sendId: this.userId,
          redaId: this.currentItem.userId,
          sendName: this.userName,
          redaName: this.currentItem.userName,
        }
      })
    },
    async handelChange(file, fileList) {
      let formData = new FormData()
      formData.append("file", file.raw);
      const res = await getUpload(formData)
      if (res !== '') {
        this.$message({
          message: "上传成功",
          type: "success",
        });
      }
      this.answerAvatar = res.data
      this.fileList = [{ name: 'food.jpg', url: res.data }]
    },
    async deleteItem(id) {

      let res = null
      if (this.userRole) {
        res = await getMinioDelete({ id: id })
      } else {
        res = await getMinioMyDelete({ id: id })
      }

      if (res.code == 0) {
        this.$message.success('删除成功')
        this.load()
      } else {
        this.$message.error('删除失败')
      }
    },
    async download(item) {
      fetch(item.image)
        .then(response => response.blob())
        .then(blob => {
          const link = document.createElement('a');
          link.href = URL.createObjectURL(blob);
          link.download = 'image.jpg';
          link.click();
          URL.revokeObjectURL(link.href);
        })
        .catch(console.error);
    },
    async comment() {
      if (this.inputValue == '') {
        this.$message.error('请输入评论内容')
        return
      }
      const { code, data } = await getQuestionAnswer({
        "answer": this.inputValue,
        "postId": this.currentItem.postId,
        answerAvatar: this.answerAvatar,
      })

      if (code == 0) {
        this.$message.success('评论成功')
        this.inputValue = ''
        this.answerAvatar = ''
        this.fileList = []
        this.answerDetail(this.currentItem)
      } else {
        this.$message.error('评论失败')
      }

    },
    handleClose(done) {
      done();
    },
    async load() {
      getQuestionList({
        current: this.currentPage,
        size: this.pageSize,
        question: this.question,
      }).then(res => {
        this.tableData = res.data.records
        this.total = Number(res.data.total)
      })
    },
    async previewPic(item) {
      this.answerDetail(item)
      this.bigImgVisible = true
    },
    async answerDetail(item) {
      const { code, data } = await getQuestionAnswerDetail({
        postId: item.postId
      })
      if (code == 0 && data) {
        this.currentItem = data
        this.commentItem = data.answers
      } else {
        this.currentItem = item
      }
    },
    async handleCurrentChange(val) {
      this.currentPage = val;
      this.load();
    }

  }
};
</script>
<style scoped lang="scss">
.Com_view {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;

  .box_cropper {
    box-shadow: 0 3px 5px 1px #cfcfcf;
    border: 10px solid white;
    width: 400px;
    height: 400px;
  }

  .box_btn {
    margin-top: 20px;
  }
}

.cropper_img {
  width: 400px;
  height: 400px;
  object-fit: contain;
}

.cropper_img_size {
  text-align: center;
}
</style>
<style scoped lang="scss">
.content1 {
  display: flex;
  /* justify-content: space-around; */
  align-items: center;
  flex-wrap: wrap;

  .item {
    width: 200px;
    height: 160px;
    position: relative;
    margin: 40px 10px 10px 10px;

    .item-img {
      width: 100%;
      height: 100%;
      object-fit: cover;
      /* 图片填充整个容器 */
      border: 1px;
      border-radius: 10px;
    }

    .title {
      width: 100%;
      height: 20px;
      background-color: #ffffff6f;
      position: absolute;
      bottom: 0;
      font-size: 18px;
      color: #000;
      text-align: center;
      border-radius: 0 0 10px 10px;
      display: flex;
      justify-content: space-around;

      .collect {
        width: 20px;
        height: 20px;
        background-image: url('../../assets/img/collect.svg');
        background-size: cover;
      }
    }
  }
}

.page {
  position: relative;
  top: 100px;
  margin-top: 20px;
}

.header {
  display: flex;
  flex-direction: row;

  .avatar {
    height: 40px;
    width: 40px;

    >img {
      height: 100%;
      width: 100%;
      border-radius: 50%;
    }
  }
}

.header-right {
  display: flex;
  flex-direction: column;
  margin-left: 10px;

  .name {
    font-size: 20px;
    font-weight: bold;
  }

  .time {
    font-size: 14px;
    color: #999;
  }
}

.description {
  display: flex;
  justify-content: space-between;
  flex-direction: column;

  >img {
    width: 300px;
    /* height: 300px;  */
  }
}

.comment-none {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  font-size: 20px;
  color: #000;
  font-weight: bold;

  >img {
    width: 200px;
    height: 200px;
  }
}

.comment-item {
  border-radius: 10px;
  margin: 10px 0;
  box-sizing: border-box;
  padding: 5px;
}
</style>
<style lang="scss" scoped>
.box-card {
  margin-bottom: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);

  &:last-child {
    margin-bottom: 0;
  }
}

// 评论输入
.pushInput {
  padding: 16px 0;

  span {
    display: block;
    margin-bottom: 12px;
    font-weight: 500;
    color: #303133;
  }

  .el-button {
    margin-top: 12px;
    transition: all 0.3s;

    &:hover {
      transform: translateY(-1px);
    }
  }
}

// 评论列表
.comment {
  margin: 16px 0;

  .comment-item {
    display: flex;

    &-right-top {
      display: flex;
      align-items: center;
      margin-bottom: 4px;
      span {
        font-size: 14px;
        color: #303133;
      }
    }

    &-right-bottom {
      span {
        font-size: 12px;
        color: #909399;
      }
    }

    &-left {
      height: 40px;
      width: 40px;
      margin-right: 12px;
      >img {
        height: 100%;
        width: 100%;
        border-radius: 50%;
      }
    }
  }
}

// 空状态
.comment-none {
  text-align: center;
  padding: 40px 0;

  div {
    margin-bottom: 20px;
    color: #c0c4cc;
  }

  img {
    width: 200px;
    opacity: 0.6;
  }
}
.comment-item-buttom{
  position: absolute;
  bottom: 0;
  right: 0;
  margin: 10px;
}
</style>